package com.openspring.platform.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.openspring.platform.dao.SecurityDAO;

@Repository("securityDAO")
public class SecurityDAOImpl implements SecurityDAO {

	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public List<Map<String, Object>> queryResourceOperators(Long resourceId) {
		String sql = "SELECT t1.*, t2.\"resourceId\" FROM emp.t_platform_operator t1 " +
				"LEFT JOIN emp.t_platform_resource_operator t2 ON t2.\"operatorId\" = t1.id AND t2.\"resourceId\" = ? " +
				"WHERE t1.status = 'enable' ORDER BY sort";
		return jdbcTemplate.queryForList(sql, resourceId);
	}

	@Override
	public int deleteResourceOperators(Long resourceId) {
		return jdbcTemplate.update("DELETE FROM emp.t_platform_resource_operator WHERE \"resourceId\" = ?", resourceId);
	}

	@Override
	public int[] insertResourceOperators(Long resourceId, Long...operatorId) {
		if(operatorId == null || operatorId.length == 0)
			return new int[]{0};
		List<Object[]> batch = new ArrayList<Object[]>();
        for(Long id : operatorId) {
            Object[] values = new Object[] {resourceId, id};
            batch.add(values);
        }
        return jdbcTemplate.batchUpdate("INSERT INTO emp.t_platform_resource_operator(\"resourceId\", \"operatorId\") VALUES (?, ?)", batch);
	}

	@Override
	public List<Map<String, Object>> queryOperatorPermissions(Long roleId) {
		String sql = "SELECT t1.name AS resource_name, t2.name AS operator_name " +
				"FROM emp.t_platform_resource t1, emp.t_platform_operator t2 " +
				"WHERE t1.id IN (SELECT \"resourceId\" FROM emp.t_platform_operator_permission WHERE \"roleId\" = ?) " +
				"AND t2.id IN (SELECT \"operatorId\" FROM emp.t_platform_operator_permission WHERE \"roleId\" = ?) " +
				"AND t1.status = 'enable' AND t2.status = 'enable' ORDER BY t1.sort, t2.sort";
		return jdbcTemplate.queryForList(sql, roleId, roleId);
	}

	@Override
	public List<Map<String, Object>> queryResourceOperatorPermissions(Long roleId, Long resourceId) {
		String sql = "SELECT t1.*, t2.*, CASE WHEN ((" +
				"SELECT \"roleId\" " +
				"FROM emp.t_platform_operator_permission " +
				"WHERE \"resourceId\" = t2.\"resourceId\" AND \"operatorId\" = t2.\"operatorId\" AND \"roleId\" = ?) " +
				"IS NULL) THEN false ELSE true END AS checked " +
			"FROM emp.t_platform_operator t1 " +
			"LEFT JOIN emp.t_platform_resource_operator t2 ON t2.\"operatorId\" = t1.id " +
			"LEFT JOIN emp.t_platform_resource t3 ON t2.\"resourceId\" = t3.id " +
			"WHERE t1.status = 'enable' AND t3.status = 'enable' AND t3.id = ? ORDER BY t1.sort";
		return jdbcTemplate.queryForList(sql, roleId, resourceId);
	}

	@Override
	public int deleteRoleOperatorPermissions(Long roleId) {
		return jdbcTemplate.update("DELETE FROM emp.t_platform_operator_permission WHERE \"roleId\" = ?", roleId);
	}

	@Override
	public int[] insertRoleOperatorPermissions(Long roleId, Long[] resourceIds,
			Long[] operatorIds) {
		List<Object[]> batch = new ArrayList<Object[]>();
		if(resourceIds != null && operatorIds != null) {
			int length = resourceIds.length;
			if(length != operatorIds.length) 
				throw new RuntimeException("参数数量错误");
			for(int i=0; i<length; i++) {
	            Object[] values = new Object[] {roleId, resourceIds[i], operatorIds[i]};
	            batch.add(values);
	        }
		}
        return jdbcTemplate.batchUpdate("INSERT INTO emp.t_platform_operator_permission(\"roleId\", \"resourceId\", \"operatorId\") VALUES (?, ?, ?)", batch);
	}
}
